package servlets;
import beans.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Connection conn;

    public void init() throws ServletException {
    	String url="jdbc:h2:d:/temp/teaching";
        String user = "your_username";
        String password = "your_password";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action == null) {
            action = "list";
        }
        switch (action) {
            case "list":
                listEmployees(request, response);
                break;
            case "add":
                showAddForm(request, response);
                break;
            case "insert":
                insertEmployee(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "update":
                updateEmployee(request, response);
                break;
            case "delete":
                updateEmployee(request, response);
                break;
            default:
                listEmployees(request, response);
                break;
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    private void listEmployees(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<employee> employees = new ArrayList<>();
        String sql = "SELECT * FROM employee";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                employee employee = new employee();
                employee.setPhoto(rs.getString("photo"));
                employee.setId(rs.getString("id"));
                employee.setName(rs.getString("name"));
                employee.setGender(rs.getString("gender"));
                employee.setOccupation(rs.getString("occupation"));
                //employee.setWorkLocation(rs.getString("work_location"));
                //employee.setWorkTime(rs.getString("work_time"));
               // employee.setPhoneNumber(rs.getString("phone_number"));
               // employee.setHireDate(rs.getDate("hire_date"));
                employees.add(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("employees", employees);
        request.getRequestDispatcher("employee_list.jsp").forward(request, response);
    }

    private void showAddForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("employee_add.jsp").forward(request, response);
    }

    private void insertEmployee(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String sql = "INSERT INTO employee (photo, id, name, gender, occupation, work_location, work_time, phone_number, hire_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, request.getParameter("photo"));
            ps.setString(2, request.getParameter("id"));
            ps.setString(3, request.getParameter("name"));
            ps.setString(4, request.getParameter("gender"));
            ps.setString(5, request.getParameter("occupation"));
            ps.setString(6, request.getParameter("work_location"));
            ps.setString(7, request.getParameter("work_time"));
            ps.setString(8, request.getParameter("phone_number"));
            ps.setDate(9, java.sql.Date.valueOf(request.getParameter("hire_date")));
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("employee?action=list");
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        employee employee = getEmployeeById(id);
        request.setAttribute("employee", employee);
        request.getRequestDispatcher("employee_edit.jsp").forward(request, response);
    }

    private employee getEmployeeById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	private void updateEmployee(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String sql = "UPDATE employee SET photo=?, name=?, gender=?, occupation=?, work_location=?, work_time=?, phone_number=?, hire_date=? WHERE id=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, request.getParameter("photo"));
            ps.setString(2, request.getParameter("name"));
            ps.setString(3, request.getParameter("gender"));
            ps.setString(4, request.getParameter("occupation"));
            ps.setString(5, request.getParameter("work_location"));
            ps.setString(6, request.getParameter("work_time"));
            ps.setString(7, request.getParameter("phone_number"));
            ps.setDate(8, java.sql.Date.valueOf(request.getParameter("hire_date")));
            ps.setString(9, request.getParameter("id"));
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
